package where2go.sctu.com.zhiyou.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.activity.ClassifyDetailActivity;
import where2go.sctu.com.zhiyou.activity.LineDetailsActivity;
import where2go.sctu.com.zhiyou.entity.ClassifyRight;
import where2go.sctu.com.zhiyou.utils.C;
import where2go.sctu.com.zhiyou.view.MyFlowLayout;

/**
 * ProjectName: Demo
 * PackageName: where2go.sctu.com.demo
 * Created by 张某人
 * CreatedTime:on 2018/5/7   11:20
 * Description:
 */

public class ClassifyRightFragmentThree extends Fragment {

    private MyFlowLayout myFlowLayout;
    private ImageView mImageView;
    private List<String> mList=new ArrayList<String>();
    private List<ClassifyRight.DataBean> dataList=new ArrayList<ClassifyRight.DataBean>();

    private String rightUrl="http://47.104.80.155/youyiyou/classtype/showThirdtype?type_Id=";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.classify_right_fragment, null);
        //得到数据
        findView(view);
        return view;
    }

    /**
     * 初始化View
     * @param view
     */
    private void findView(View view) {
        myFlowLayout = (MyFlowLayout) view.findViewById(R.id.mFlowLayout);
        mImageView=(ImageView)view.findViewById(R.id.img_classify) ;
        mImageView.setImageResource(R.drawable.learn);

       loaderData(rightUrl+223);

    }




    private void loaderData(String url){

        StringRequest request=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                ClassifyRight classifyRight=gson.fromJson(response,ClassifyRight.class);
                dataList=classifyRight.getData();
                //将typeName放进List
                for (int i = 0; i <dataList.size() ; i++) {
                    mList.add(dataList.get(i).getTypeName());
                }
                myFlowLayout.addData(mList);
                myFlowLayout.setFlowLayoutListener(new MyFlowLayout.FlowLayoutListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent =new Intent();
                        int typeId=dataList.get(position).getTypeId();
                        intent.setClass(getActivity(), ClassifyDetailActivity.class);
                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        bundle.putString("typeId", String.valueOf(typeId));
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }
                });


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        });

        MyApplication.getHttpQueues().add(request);
    }

}
